﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStruct
{

    /// <summary>
    /// 队列--链式实现
    /// </summary>
    internal class LinkedQueue
    {
        Node front;//队头
        Node rear;//队尾
        /// <summary>
        /// 初始化一个空队列
        /// </summary>
        public LinkedQueue()
        {
            front = null;
            rear = null;
        }

        /// <summary>
        /// 入队
        /// </summary>
        /// <param name="data"></param>
        public void EnQueue(int data)
        {
            Node newNode = new Node(data);
            if (front == null)
            {
                front = newNode;
                rear = newNode;
            }
            else
            {
                rear.Next = newNode;
                rear = newNode;
            }
        }

        /// <summary>
        /// 出队
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public Node Dequeue()
        {
            if (IsEmpty())
            {
                throw new Exception("队列为空！");
            }
            Node res = front;
            front = front.Next;
            return res;
        }

        /// <summary>
        /// 队列是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return front == null;
        }

        public void Print()
        {
            Node currentNode = front;
            while (currentNode != null)
            {
                Console.Write(currentNode.Data + " ");
                currentNode = currentNode.Next;
            }
            Console.WriteLine($"队头:{front?.Data},队尾：{rear?.Data}");
        }

        /// <summary>
        /// 查看队头元素
        /// </summary>
        /// <returns></returns>
        public Node Peek()
        {
            return front;
        }
    }
}
